

***************************************************************************************************
*******************************CLEANING THE GEOCODED AFROBAROMETER DATA****************************
***************************************************************************************************






global afrobar="C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer"
global china_ethnic= "C:\Users\xisaka\Dropbox\Ias\MINA PAPERS\chinese aid and ethnic sentiments\stata"
global chinadropbox="C:\Users\xisaka\Dropbox\Projekt\Gendergap\Aid_China"


set more off
set matsize 800




*******************************WAVE 3*************************************


set more off
set matsize 800


use "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\afb_full_r3.dta", clear

gen wave=3
cap replace country= proper(country)

foreach var of varlist _all {
cap	replace `var'="." if `var'=="#NULL!"
cap	replace `var'="." if `var'=="-1"
cap	destring `var', replace

} 

rename country c_numb
gen country="Benin" if c_numb==1
replace country="Botswana" if c_numb==2
replace country="Cape Verde" if c_numb==3
replace country="Ghana" if c_numb==4
replace country="Kenya" if c_numb==5
replace country="Lesotho" if c_numb==6
replace country="Madagascar" if c_numb==7
replace country="Malawi" if c_numb==8
replace country="Mali" if c_numb==9
replace country="Mozambique" if c_numb==10
replace country="Namibia" if c_numb==11
replace country="Nigeria" if c_numb==12
replace country="Senegal" if c_numb==13
replace country="South Africa" if c_numb==14
replace country="Tanzania" if c_numb==15
replace country="Uganda" if c_numb==16
replace country="Zambia" if c_numb==17
replace country="Zimbabwe" if c_numb==18


	// date variables

split dateintr, p("-")
gen month=.

replace month=1 if dateintr2=="Jan"
replace month=2 if dateintr2=="Feb"
replace month=3 if dateintr2=="Mar"
replace month=4 if dateintr2=="Apr"
replace month=5 if dateintr2=="May"
replace month=6 if dateintr2=="Jun"
replace month=7 if dateintr2=="Jul"
replace month=8 if dateintr2=="Aug"
replace month=9 if dateintr2=="Sep"
replace month=10 if dateintr2=="Oct"
replace month=11 if dateintr2=="Nov"
replace month=12 if dateintr2=="Dec"

destring dateintr3, gen(year)
replace year=year+2000
rename dateintr1 day

	// Generate a new age variable, with DK and refused missing
gen age = q1 if q1<900 & q1>=18 				
label var age "age of respondent"
gen age2 = age*age/100								// age squared
label var age2 "age squared /100"

	// Urban/rural
tab urbrur, gen(ur)
rename ur1 urban
rename ur2 rural
label var urban "Urban dummy"
label var rural "Rural dummy"

	// gender
gen female = q101==2
label var female "Female dummy"
gen male = q101==1
label var male "Male dummy"

	// Education 
gen education = q90 if q90>=0 & q90<=9
label def education 0 "No edu" 1 "Informal only" 2 "Some Primary" 3 "Primary completed" 4 "Some secondary" 5 "Secondary completed" 6 "Some post-secondary, non-uni" 7 "Some Uni" 8 "Uni completed" 9 "Post-graduate", replace
label val education education

	// FIXING THE WEIGHT VARIABLES

destring withinwt, replace dpcomma
rename acroswt acrosswt
rename combwt combinwt
destring acrosswt, replace dpcomma
destring combinwt, replace dpcomma

	// Ethnic groups

recode q79 (-1=.) (170/178=.)(990/999=.), gen (ethnic)			//170-178 non ethnic response categories (in cape verde)

/*ethnic variables nov 2018*/
/*Value Labels: 1=I Feel Only (r’s group), 2=I Feel More (r’s group) than [Ghanaian/Kenyan/etc.], 3=I Feel Equally
[Ghanaian/Kenyan/etc.] and (r’s groups), 4=I Feel More [Ghanaian/Kenyan/etc.] than (r’s groups), 5=I feel only
[Ghanaian/Kenyan/etc.], 7=Not Applicable, 9=Don’t Know, 98=Refused to Answer, -1=Missing Data*/

tab q82
gen ethnicidentity=cond(q82==1 | q82==2,1,0) 				/*dummy for reporting to have a stronger ethnic than national identity*/
replace ethnicidentity=. if ( (q82==-1) | (q82>10) )		
tab ethnicidentity											

gen ordinal=q82
replace ordinal=. if q82>5 
gen ethid_ordinal=6-(ordinal)					//recode to make it increasing in ethnic identity

	// COETHNIC WITH PRESIDENT*

/*based on externally compiled data on the ethnic background of the president at the time of the survey (been president for at least a year at the time of the survey). 
consult at least 2 sources for each country. usually encyclopedia britannica and wikipedia. note: on top of lesotho and capeverde, also missing for  tanzania and zambia*/
gen presethnic = 	( (country=="Benin" 	& q79==122)				///			
			| (country=="Botswana" 			& q79==142)				///						
			| (country=="Ghana" 			& q79==180)				///					
			| (country=="Kenya" 			& q79==200)				///					
			| (country=="Madagascar" 		& q79==253)				///				
			| (country=="Malawi" 			& q79==266)				///			
			| (country=="Mali" 				& q79==281)				///				
			| (country=="Mozambique" 		& q79==300)				///				
			| (country=="Namibia" 			& q79==320)				///					
			| (country=="Nigeria" 			& q79==342)				///				
			| (country=="Senegal" 			& q79==360)				///				
			| (country=="SouthAfrica" 		& q79==102)				///					
			| (country=="Uganda" 			& q79==510)				///				
			| (country=="Zambia" 			& q79==431)	)									
replace presethnic = . if ((country=="Lesotho") | (country=="CapeVerde") | (country=="Tanzania") | (country=="Zimbabwe") )




keep wave country region respno withinwt combinwt day dateintr2 dateintr3 year month age age2 urban rural female male ethnic ethnicidentity presethnic ethid_ordinal geoname_id-geographic_exactness

compress
duplicates list respno
duplicates drop respno, force

save "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\r3_short_new.dta", replace




/* Fix country names:

drop country 
merge 1:1 respno using "C:\Users\xisaka\Dropbox\Projekt\Gendergap\Aid_China\Andreas filer ny afro\\Fixed data\r3_short.dta", keepusing(country)


save "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\r3_short_new.dta", replace
*/








********************WAVE 4*************************



set more off
set matsize 800


use "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\afb_full_r4.dta", clear


gen wave=4
cap replace country= proper(country)

foreach var of varlist _all {
cap	replace `var'="." if `var'=="#NULL!"
cap	replace `var'="." if `var'=="-1"
cap	destring `var', replace

} 

rename country c_numb
gen country="Benin" if c_numb==1
replace country="Botswana" if c_numb==2
replace country="Burkina Faso" if c_numb==3
replace country="Cape Verde" if c_numb==4
replace country="Ghana" if c_numb==5
replace country="Kenya" if c_numb==6
replace country="Lesotho" if c_numb==7
replace country="Liberia" if c_numb==8
replace country="Madagascar" if c_numb==9
replace country="Malawi" if c_numb==10
replace country="Mali" if c_numb==11
replace country="Mozambique" if c_numb==12
replace country="Namibia" if c_numb==13
replace country="Nigeria" if c_numb==14
replace country="Senegal" if c_numb==15
replace country="South Africa" if c_numb==16
replace country="Tanzania" if c_numb==17
replace country="Uganda" if c_numb==18
replace country="Zambia" if c_numb==19
replace country="Zimbabwe" if c_numb==20



	// date variables

gen date=date(dateintr, "YMD")
split dateintr, p("-")
gen month=.

replace month=1 if dateintr2=="Jan"
replace month=2 if dateintr2=="Feb"
replace month=3 if dateintr2=="Mar"
replace month=4 if dateintr2=="Apr"
replace month=5 if dateintr2=="May"
replace month=6 if dateintr2=="Jun"
replace month=7 if dateintr2=="Jul"
replace month=8 if dateintr2=="Aug"
replace month=9 if dateintr2=="Sep"
replace month=10 if dateintr2=="Oct"
replace month=11 if dateintr2=="Nov"
replace month=12 if dateintr2=="Dec"

destring dateintr3, gen(year)
replace year=year+2000

rename dateintr1 day


	// FIXING THE WEIGHT VARIABLES

destring withinwt, replace dpcomma
destring acrosswt, replace dpcomma
destring combinwt, replace dpcomma


	//  Ethnic groups ´

recode q79 (-1=.) (220/229=.)(990/999=.), gen (ethnic)			//220-229 non ethnic response categories (in cape verde)


	// Generate a new age variable, with DK and refused missing
gen age = q1 if q1<900 & q1>=18 				
label var age "age of respondent"
gen age2 = age*age/100								// age squared
label var age2 "age squared /100"

	// Urban/rural
tab urbrur, gen(ur)
rename ur1 urban
rename ur2 rural
label var urban "Urban dummy"
label var rural "Rural dummy"

	// gender
gen female = q101==2
label var female "Female dummy"
gen male = q101==1
label var male "Male dummy"

	// Education 
gen education = q89 if q89>=0 & q89<=9
label def education 0 "No edu" 1 "Informal only" 2 "Some Primary" 3 "Primary completed" 4 "Some secondary" 5 "Secondary completed" 6 "Some post-secondary, non-uni" 7 "Some Uni" 8 "Uni completed" 9 "Post-graduate", replace
label val education education


	// ethnic variables

/*1=I feel only (R’s ethnic group), 2=I feel more (R’s ethnic group) than [Ghanaian/Kenyan/etc.], 3=I
feel equally [Ghanaian/Kenyan/etc.] and (R’s ethnic group), 4=I feel more [Ghanaian/Kenyan/etc.] than (R’s ethnic
group), 5=I feel only [Ghanaian/Kenyan/etc.], 7=Not applicable, 9=Don’t know, 998=Refused to answer, -1=Missing data*/

gen ethnicidentity=cond(q83==1 | q83==2,1,0) if q83~=(-1)	/*dummy for reporting to have a stronger ethnic than national identity*/
tab ethnicidentity						
bysort country: tab ethnicidentity		

gen ordinal=q83
replace ordinal=. if q83>5 | q83<0
tab ordinal
gen ethid_ordinal=6-(ordinal)					//recode to make it increasing in ethnic identity


	// COETHNIC WITH PRESIDENT
	
/*based on externally compiled data on the ethnic background of the president at the time of the survey (been president for at least a year at the time of the survey). 
consult at least 2 sources for each country. usually encyclopedia britannica and wikipedi*/
gen presethnic = 	( (country=="Benin" 	& q79==104)				///			
			| (country=="Botswana" 			& q79==142)				///			
			| (country=="BurkinaFaso" 		& q79==182)				///			
			| (country=="Ghana" 			& q79==260)				///			
			| (country=="Kenya" 			& q79==300)				///			
			| (country=="Liberia" 			& q79==385)				///	
			| (country=="Madagascar" 		& q79==432)				///	
			| (country=="Malawi" 			& q79==466)				///
			| (country=="Mali" 				& q79==512)				///				
			| (country=="Mozambique" 		& q79==540)				///				
			| (country=="Namibia" 			& q79==580)				///			
			| (country=="Nigeria" 			& q79==625)				///			
			| (country=="Senegal" 			& q79==660)				///			
			| (country=="SouthAfrica" 		& q79==703)				///				
			| (country=="Uganda" 			& q79==781)				///			
			| (country=="Zimbabwe" 			& q79==861))									
replace presethnic = . if ((country=="Lesotho") | (country=="CapeVerde") | (country=="Tanzania") | (country=="Zambia"))





keep wave country region respno withinwt combinwt day dateintr2 dateintr3 year month age age2 urban rural female male ethnic ethnicidentity presethnic ethid_ordinal geoname_id-geographic_exactness


compress


save "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\r4_short_new.dta", replace








********************WAVE 5*************************




set more off
set matsize 800


use "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\afb_full_r5.dta", clear


gen wave=5
cap replace country= proper(country)

foreach var of varlist _all {
cap	replace `var'="." if `var'=="#NULL!"
cap	replace `var'="." if `var'=="-1"
cap	destring `var', replace

} 


	// date variables

gen date=date(dateintr, "YMD")
split dateintr, p("-")
gen month=.
replace month=1 if dateintr2=="Jan"
replace month=2 if dateintr2=="Feb"
replace month=3 if dateintr2=="Mar"
replace month=4 if dateintr2=="Apr"
replace month=5 if dateintr2=="May"
replace month=6 if dateintr2=="Jun"
replace month=7 if dateintr2=="Jul"
replace month=8 if dateintr2=="Aug"
replace month=9 if dateintr2=="Sep"
replace month=10 if dateintr2=="Oct"
replace month=11 if dateintr2=="Nov"
replace month=12 if dateintr2=="Dec"
destring dateintr3, gen(year)
replace year=year+2000

rename dateintr1 day


rename country_alpha c_numb			//in round 5, it is country_alpha that is numbered in alphabetical order. the ordinary country var has new numbers
rename country country1, replace 	//rename the strangely numbered country var to country1, so we can use country for the country names below

*tostring country, replace
gen country="Algeria" if c_numb==1
replace country="Benin" if c_numb==2
replace country="Botswana" if c_numb==3
replace country="Burkina Faso" if c_numb==4
replace country="Burundi" if c_numb==5
replace country="Cameroon" if c_numb==6
replace country="Cape Verde" if c_numb==7
replace country="Cote d'Ivoire" if c_numb==8
replace country="Egypt" if c_numb==9
replace country="Ghana" if c_numb==11
replace country="Guinea" if c_numb==12
replace country="Kenya" if c_numb==13
replace country="Lesotho" if c_numb==14
replace country="Liberia" if c_numb==15
replace country="Madagascar" if c_numb==16
replace country="Malawi" if c_numb==17
replace country="Mali" if c_numb==18
replace country="Mauritius" if c_numb==19
replace country="Morocco" if c_numb==20
replace country="Mozambique" if c_numb==21
replace country="Namibia" if c_numb==22
replace country="Niger" if c_numb==23
replace country="Nigeria" if c_numb==24
replace country="Senegal" if c_numb==25
replace country="Sierra Leone" if c_numb==26
replace country="South Africa" if c_numb==27
replace country="Sudan" if c_numb==28
replace country="Swaziland" if c_numb==29
replace country="Tanzania" if c_numb==30
replace country="Togo" if c_numb==31
replace country="Tunisia" if c_numb==32
replace country="Uganda" if c_numb==33
replace country="Zambia" if c_numb==34
replace country="Zimbabwe" if c_numb==35


	// FIXING THE WEIGHT VARIABLES

destring withinwt, replace dpcomma
*destring acrosswt, replace dpcomma
destring combinwt, replace dpcomma

	// Ethnic groups
recode q84 (-1=.) (9900/9999=.), gen (ethnic)

	// Generate a new age variable, with DK and refused missing
gen age = q1 if q1<106 & q1>=18 				
label var age "age of respondent"
gen age2 = age*age/100								// age squared
label var age2 "age squared /100"

	// Urban/rural
* Semi-urban to urban
recode urbrur (3=1)
tab urbrur, gen(ur)
rename ur1 urban
rename ur2 rural
label var urban "Urban dummy"
label var rural "Rural dummy"

	// gender
gen female = q101==2
label var female "Female dummy"
gen male = q101==1
label var male "Male dummy"


	// Education 
gen education = q97 if q97>=0 & q97<=9
label def education 0 "No edu" 1 "Informal only" 2 "Some Primary" 3 "Primary completed" 4 "Some secondary" 5 "Secondary completed" 6 "Some post-secondary, non-uni" 7 "Some Uni" 8 "Uni completed" 9 "Post-graduate"
label val education education


	// ethnic variables 
	
codebook q85b
gen ethnicidentity=cond(q85b==1 | q85b==2,1,0)			/*dummy for reporting to have a stronger ethnic than national identity*/
replace ethnicidentity=. if q85b<0 | q85b>10	


gen ordinal=q85b
replace ordinal=. if q85b>5 | q85b<0
tab ordinal
gen ethid_ordinal=6-(ordinal)					//recode to make it increasing in ethnic identity


/*pressethnic is a dummy for belonging to the same ethnic group as the country president (or top piolitical leader. 
based on self-reported group affiliation coupled with externally compiled data on the ethnic background of the president at the time of the survey.  
for ethnic group of the president, we consult at least 2 sources for each country, drawing most heavily on the compilation in Dreher et al (2015), 
when necessary updated with more recent data from other sources (e.g. encyclopedia britannica, wikipedia, aljazeera, washington post, africareview.com etc. ).*/
/*Notes: 
**ethnic group of president not found for lesotho and mali.
**for mauritius we use the prime minister - chief over the executive branch of government.
**only 11 respondents in the presidents group in tanzania
**in cote d'Ivoire the president belong to the Dioula group. this group is a subgroup of the mande people. in the survey there is mande du nord and mande du sud. 
**self-reported group affiliation not available for Swaziland, Algeria, Egypt, Morocco, Sudan, and Tunisia. Moreover, in cape verde the question is not anwered in ethnic terms but rather in terms of gender, age and occupation
*/
	

gen presethnic = 	((country=="Benin" 			& q84==104) 				/// 
			| (country=="Botswana" 			& q84==142)						///
			| (country=="Burkina Faso"		& q84==180)						///
			| (country=="Ghana"	 			& q84==260)						/// 
			| (country=="Kenya" 			& q84==300)						///
			| (country=="Liberia" 			& q84==385)						///		
			| (country=="Madagascar" 		& q84==432)						///
			| (country=="Malawi" 			& q84==464)						///
			| (country=="Mozambique" 		& q84==540)						///			
			| (country=="Namibia" 			& q84==580)						///
			| (country=="Nigeria" 			& q84==631)						///
			| (country=="Senegal" 			& q84==661)						///
			| (country=="South Africa" 		& q84==710)						///			
			| (country=="Tanzania" 			& q84==744)						///
			| (country=="Uganda" 			& q84==781)						///
			| (country=="Zambia" 			& q84==820)						///
			| (country=="Zimbabwe" 			& q84==861)						///
			| (country=="Mauritius" 		& q84==903)						///
			| (country=="Sierra Leone" 		& q84==942)						///
			| (country=="Niger" 			& q84==1100)					/// 
			| (country=="Togo" 				& q84==1142)					///
			| (country=="Burundi"			& q84==1180)					///
			| (country=="Cameroon"			& q84==1220)					///
			| (country=="Cote d'Ivoire" 	& q84==1262)					/// 
			| (country=="Guinea" 			& q84==1302)	)				//	 
replace presethnic=. if 	q84==. |	country=="Mali" | country=="Lesotho"	| country=="Swaziland"	 	| country=="Sudan"		///
| country=="Algeria"	| country=="Egypt"	| country=="Morocco"	| country=="Tunisia"				
		 
		



keep wave country region respno withinwt combinwt day dateintr2 dateintr3 year month age age2 urban rural female male ethnic ethnicidentity presethnic ethid_ordinal geoname_id-geographic_exactness

compress

*4 observations have same respno, drop
duplicates list respno
duplicates drop respno, force


save "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\r5_short_new.dta", replace












********************WAVE 6*************************




use "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\afb_full_r6", clear


gen wave=6

cap replace country= proper(country)

foreach var of varlist _all {
cap	replace `var'="." if `var'=="#NULL!"
cap	replace `var'="." if `var'=="-1"
cap	destring `var', replace

} 


rename country c_numb
gen country="Algeria" if c_numb==1
replace country="Benin" if c_numb==2
replace country="Botswana" if c_numb==3
replace country="Burkina Faso" if c_numb==4
replace country="Burundi" if c_numb==5
replace country="Cameroon" if c_numb==6
replace country="Cape Verde" if c_numb==7
replace country="Cote d'Ivoire" if c_numb==8
replace country="Egypt" if c_numb==9
replace country="Gabon" if c_numb==10
replace country="Ghana" if c_numb==11
replace country="Guinea" if c_numb==12
replace country="Kenya" if c_numb==13
replace country="Lesotho" if c_numb==14
replace country="Liberia" if c_numb==15
replace country="Madagascar" if c_numb==16
replace country="Malawi" if c_numb==17
replace country="Mali" if c_numb==18
replace country="Mauritius" if c_numb==19
replace country="Morocco" if c_numb==20
replace country="Mozambique" if c_numb==21
replace country="Namibia" if c_numb==22
replace country="Niger" if c_numb==23
replace country="Nigeria" if c_numb==24
replace country="São Tomé and Príncipe" if c_numb==25
replace country="Senegal" if c_numb==26
replace country="Sierra Leone" if c_numb==27
replace country="South Africa" if c_numb==28
replace country="Sudan" if c_numb==29
replace country="Swaziland" if c_numb==30
replace country="Tanzania" if c_numb==31
replace country="Togo" if c_numb==32
replace country="Tunisia" if c_numb==33
replace country="Uganda" if c_numb==34
replace country="Zambia" if c_numb==35
replace country="Zimbabwe" if c_numb==36


	// date variables

split dateintr, p("-")						//generates 3 new  variables, split by -

gen month=.

replace month=1 if dateintr2=="Jan"
replace month=2 if dateintr2=="Feb"
replace month=3 if dateintr2=="Mar"
replace month=4 if dateintr2=="Apr"
replace month=5 if dateintr2=="May"
replace month=6 if dateintr2=="Jun"
replace month=7 if dateintr2=="Jul"
replace month=8 if dateintr2=="Aug"
replace month=9 if dateintr2=="Sep"
replace month=10 if dateintr2=="Oct"
replace month=11 if dateintr2=="Nov"
replace month=12 if dateintr2=="Dec"

destring dateintr3, gen(year)
replace year=year+2000

rename dateintr1 day


	// fixing the weight variables

destring withinwt, replace dpcomma
*destring acrosswt, replace dpcomma		//not in round 6
destring combinwt, replace dpcomma


	// Ethnic groups 
recode q87 (-1=.) (9900/9999=.), gen (ethnic)
replace ethnic=. if ethnic==99					//code for 'not asked in country'


****age variable, with DK and refused missing
gen age = q1 if q1<106 & q1>=18 				
label var age "age of respondent"
gen age2 = age*age/100							// age squared
label var age2 "age squared /100"


	// Urban/rural
* Semi-urban to urban
recode urbrur (3=1)
tab urbrur, gen(ur)
rename ur1 urban
rename ur2 rural
label var urban "Urban dummy"
label var rural "Rural dummy"


	// gender
gen female = q101==2
label var female "Female dummy"
gen male = q101==1
label var male "Male dummy"


	// Education 
gen education = q97 if q97>=0 & q97<=9
label def education 0 "No edu" 1 "Informal only" 2 "Some Primary" 3 "Primary completed" 4 "Some secondary" 5 "Secondary completed" 6 "Some post-secondary, non-uni" 7 "Some Uni" 8 "Uni completed" 9 "Post-graduate"
label val education education

	// ethnic variables 
codebook q88b
gen ethnicidentity=cond(q88b==1 | q88b==2,1,0)			/*dummy for reporting to have a stronger ethnic than national identity*/
replace ethnicidentity=. if q88b<0 | q88b>10	

gen ordinal=q88b
replace ordinal=. if q88b>5 | q88b<0
tab ordinal
gen ethid_ordinal=6-(ordinal)					//recode to make it increasing in ethnic identity


/*pressethnic is a dummy for belonging to the same ethnic group as the country president (or top piolitical leader. 
based on self-reported group affiliation coupled with externally compiled data on the ethnic background of the president at the time of the survey.  
for ethnic group of the president, we consult at least 2 sources for each country, drawing most heavily on the compilation in Dreher et al (2015), 
when necessary updated with more recent data from other sources (e.g. encyclopedia britannica, wikipedia, aljazeera, washington post, africareview.com etc. ).*/

gen presethnic = 	(	(country=="Benin" 			& q87==45) 		///	Benin
| (country=="Botswana" 				& q87==	142)						///	Botswana
| (country=="Burkina Faso" 			& q87==	180)						///	Burkina Faso
| (country=="Cameroon" 				& q87==	1220)						///	Cameroon
| (country=="Ivory Coast" 			& q87==	1262)						///	Ivory Coast
| (country=="Ghana" 				& q87==	271)						///	Ghana
| (country=="Guinea" 				& q87==	23)							///	Guinea
| (country=="Kenya" 				& q87==	300)						///	Kenya
| (country=="Liberia" 				& q87==	385)						///	Liberia
| (country=="Malawi" 				& q87==	464)						///	Malawi
| (country=="Mauritius" 			& q87==	903)						///	Mauritius
| (country=="Mozambique" 			& q87==	547)						///	Mozambique
| (country=="Namibia" 				& q87==	580)						///	Namibia
| (country=="Niger" 				& q87==	16)							///	Niger
| (country=="Nigeria" 				& q87==	631)						///	Nigeria
| (country=="Senegal" 				& q87==	661)						///	Senegal
| (country=="Sierra Leone" 			& q87==	942)						///	Sierra Leone
| (country=="South Africa" 			& q87==	46)							///	South Africa
| (country=="Tanzania" 				& q87==	744)						///	Tanzania
| (country=="Togo" 				 	& q87==	1142)						///	Togo
| (country=="Uganda" 				& q87==	781)						///	Uganda
| (country=="Zambia" 				& q87==	820)						///	Zambia
| (country=="Zimbabwe" 				& q87==	861	) )						/*	Zimbabwe*/
replace presethnic=. if 	country=="Burundi" | country=="Cape Verde" | country=="Egypt" | country=="Gabon"	| country=="Lesotho" | country=="Madagascar" ///
	| country=="Mali"	| country=="Morocco" | country=="São Tomé and Príncipe"	| country=="Sudan" | country=="Swaziland" | country=="Tunisia"

	




keep wave country region respno withinwt combinwt day dateintr2 dateintr3 year month age age2 urban rural female male ethnic ethnicidentity presethnic ethid_ordinal geoname_id-geographic_exactness

compress



save "C:\Users\xisaka\ShareFile\Personal Folders\Afrobarometer\r6_short_new.dta", replace









***************************************************************************************************
************************************APPENDING AND FURTHER CLEANING*********************************
***************************************************************************************************




use "$afrobar/r3_short_new.dta", clear


append using "$afrobar/r4_short_new.dta"
append using "$afrobar/r5_short_new.dta"
append using "$afrobar/r6_short_new.dta"





*******************************RESTRICTING TO PRECISION CODE 1

*only 56% with precision code 1. restrict to these in a first step. create alternative dataset retaining more later
tab precision_code
keep if precision_code==1				//keep only the most precisely geocoded afrobar observations

rename year int_year
*br if int_year==.						//many missing in senegal wave 2
replace int_year=2002 if wave==2 & country=="Senegal" // Source: Afrobarometer.org

drop if int_year==.
tab wave int_year

replace country="Cote D'Ivoire" if country=="Cote d'Ivoire"		//capital D


** Generate country codes
*ssc install kountry
kountry country, from(other) stuck
rename _ISO3N_ countrycode

tab country if countrycode==.		// sao tome and pricipe has no country code, its code is 678 acc to https://www.iso.org/
tab countrycode						
replace countrycode=678 if country=="São Tomé and Príncipe"

*group variable for observations with the same lat and long 
egen cluster=group(latitude longitude)

rename latitude g_lat
rename longitude g_lon


compress
save "$afrobar/Afro_tot_precisioncode1_replication.dta", replace



* Make a dataset at the cluster level:
collapse (first) country countrycode cluster, by(g_lat g_lon)
save "$afrobar/Afro_collapsed_precisioncode1_replication.dta", replace







*******************************INCLUDING PRECISION CODE 1-3



use "$afrobar/r3_short_new.dta", clear
append using "$afrobar/r4_short_new.dta"
append using "$afrobar/r5_short_new.dta"
append using "$afrobar/r6_short_new.dta"



tab precision_code
keep if precision_code<4				//keep precision code 1-3

rename year int_year
*br if int_year==.						//many missing in senegal wave 2
replace int_year=2002 if wave==2 & country=="Senegal" // Source: Afrobarometer.org

drop if int_year==.

replace country="Cote D'Ivoire" if country=="Cote d'Ivoire"		//capital D


** Generate country codes
*ssc install kountry
kountry country, from(other) stuck
rename _ISO3N_ countrycode

replace countrycode=678 if country=="São Tomé and Príncipe" 	//(https://www.iso.org/)


*group variable for observations with the same lat and long 
egen cluster=group(latitude longitude)

rename latitude g_lat
rename longitude g_lon


compress
save "$afrobar/Afro_tot_precisioncode123_replication.dta", replace



* Make a dataset at the cluster level:
collapse (first) country countrycode cluster, by(g_lat g_lon)
save "$afrobar/Afro_collapsed_precisioncode123_replication.dta", replace











****





